package mylab.io.store.mqtt.common;

import lombok.Getter;
import lombok.Setter;
import mylab.io.base.model.BaseMsg;
import mylab.utils.common.ByteUtil;


@Getter
@Setter
public class MqttMsg extends BaseMsg {
    MqttQoS qos = MqttQoS.AT_LEAST_ONCE;
    String topic;

    public MqttMsg() {
    }

    public MqttMsg(String topic, String payload) {
        this(topic, payload, MqttQoS.AT_LEAST_ONCE);
    }

    public MqttMsg(String topic, byte[] payload) {
        this(topic, payload, MqttQoS.AT_LEAST_ONCE);
    }

    public MqttMsg(String topic, String payload, MqttQoS qos) {
        this(topic, ByteUtil.stringToBytes(payload), qos);
    }

    public MqttMsg(String topic, byte[] payload, MqttQoS qos) {
        super();
        this.topic = topic;
        this.payload = payload;
        this.qos = qos;

        setCode(topic);
    }

    public String getPayloadAsString() {
        return ByteUtil.bytesToString(payload);
    }
}
